package service

import (
	"blog_go/internal/model"
	"blog_go/internal/repository"
	"errors"
	"mime/multipart"
)

type ArticleService interface {
	QueryAllArticle(categoryId int, tagId int, userId int, page int, pageSize int) (*model.ArticleResult, error)
	QueryUserArticle(userId int, targertUserId int, page int, pageSize int) (*model.ArticleResult, error)
	QueryArticle(userId int, userType int) ([]*model.ArticleView, error)
	QueryArticles(keyWords string, page int, pageSize int, userId int) (*model.ArticleResult, error)
	QueryArticleByArticleId(articleId int, userId int) (*model.ArticleView, error)
	QueryCollectedArticle(userId int, collectionId int, page int, pageSize int) (*model.ArticleResult, error)
	AddArticle(article model.ArticleView, userId int) error
	AddMdArticle(article model.ArticleView, userId int) error
	UpdateArticle(article model.ArticleView, userId int) error
	UpdateMdArticle(article model.ArticleView, userId int) error
	UpdateUploadFile(files *multipart.Form) (string, error)
	DelArticle(articleIds []int, userId int, userType int) error
	LikeArticle(articleId int, userId int, isLike bool) error
	DisLikeArticle(articleId int, userId int, isDisLike bool) error
	CollectArticle(articleId int, userId int, isCollect bool, collectionId int) error
	ViewArticle(articleId int) error
	ReviewArticle(articleIds []int, status int) error
	ReviewAllArticle() error
	MoveArticle(articleId int, collectionId int) error
}

type articleService struct {
	*Service
	articleRepository repository.ArticleRepository
}

func NewArticleService(service *Service, articleRepository repository.ArticleRepository) ArticleService {
	return &articleService{
		Service:           service,
		articleRepository: articleRepository,
	}
}

func (s *articleService) AddMdArticle(article model.ArticleView, userId int) error {
	if article.AuthorId != userId {
		return errors.New("用户Id和作者Id不匹配")
	}
	if article.Title == "" || article.MarkdownContent == "" || article.TagIds == nil {
		return errors.New("参数错误")
	}

	return s.articleRepository.AddMdArticle(article)
}

func (s *articleService) AddArticle(article model.ArticleView, userId int) error {
	if article.AuthorId != userId {
		return errors.New("用户Id和作者Id不匹配")
	}
	if article.Title == "" || article.Content == "" || article.TagIds == nil {
		return errors.New("参数错误")
	}

	return s.articleRepository.AddArticle(article)
}

func (s *articleService) UpdateUploadFile(files *multipart.Form) (string, error) {
	if len(files.File["files"]) == 0 {
		return "", errors.New("没有上传文件")
	}
	// 设置文件大小限制
	const maxFileSize = 10 << 20 // 10 MB

	// 检查文件大小
	if files.File["files"][0].Size > maxFileSize {
		return "", errors.New("上传文件过大,应该在10MB以内")
	}

	return s.articleRepository.UpdateUploadFile(files)
}

func (s *articleService) UpdateArticle(article model.ArticleView, userId int) error {
	if article.ArticleId == 0 {
		return errors.New("文章id不能为空")
	}
	return s.articleRepository.UpdateArticle(article, userId)
}

func (s *articleService) UpdateMdArticle(article model.ArticleView, userId int) error {
	if article.ArticleId == 0 {
		return errors.New("文章id不能为空")
	}
	return s.articleRepository.UpdateMdArticle(article, userId)
}

func (s *articleService) DelArticle(articleIds []int, userId int, userType int) error {
	if articleIds == nil {
		return errors.New("参数错误")
	}

	return s.articleRepository.DelArticle(articleIds, userId, userType)
}

func (s *articleService) QueryArticle(userId int, userType int) ([]*model.ArticleView, error) {
	return s.articleRepository.QueryArticle(userId, userType)
}

func (s *articleService) QueryArticles(keyWords string, page int, pageSize int, userId int) (*model.ArticleResult, error) {
	return s.articleRepository.QueryArticles(keyWords, page, pageSize, userId)
}

func (s *articleService) QueryAllArticle(categoryId int, tagId int, userId int, page int, pageSize int) (*model.ArticleResult, error) {
	return s.articleRepository.QueryAllArticle(categoryId, tagId, userId, page, pageSize)
}

func (s *articleService) QueryArticleByArticleId(articleId int, userId int) (*model.ArticleView, error) {
	return s.articleRepository.QueryArticleByArticleId(articleId, userId)
}

func (s *articleService) LikeArticle(articleId int, userId int, isLike bool) error {
	if isLike {
		return s.articleRepository.LikeArticle(articleId, userId)
	} else {
		return s.articleRepository.UnLikeArticle(articleId, userId)
	}
}

func (s *articleService) DisLikeArticle(articleId int, userId int, isDisLike bool) error {
	if isDisLike {
		return s.articleRepository.DisLikeArticle(articleId, userId)
	} else {
		return s.articleRepository.UnDisLikeArticle(articleId, userId)
	}
}

func (s *articleService) CollectArticle(articleId int, userId int, isCollect bool, collectionId int) error {
	if isCollect {
		return s.articleRepository.CollectArticle(articleId, userId, collectionId)
	} else {
		return s.articleRepository.UnCollectArticle(articleId, userId)
	}
}

func (s *articleService) ViewArticle(articleId int) error {
	return s.articleRepository.ViewArticle(articleId)
}

func (s *articleService) ReviewArticle(articleIds []int, status int) error {
	return s.articleRepository.ReviewArticle(articleIds, status)
}

func (s *articleService) MoveArticle(articleId int, collectionId int) error {
	return s.articleRepository.MoveArticle(articleId, collectionId)
}

func (s *articleService) QueryCollectedArticle(userId int, collectionId int, page int, pageSize int) (*model.ArticleResult, error) {
	return s.articleRepository.QueryCollectedArticle(userId, collectionId, page, pageSize)
}

func (s *articleService) QueryUserArticle(userId int, targertUserId int, page int, pageSize int) (*model.ArticleResult, error) {
	return s.articleRepository.QueryUserArticle(userId, targertUserId, page, pageSize)
}

func (s *articleService) ReviewAllArticle() error {
	return s.articleRepository.ReviewAllArticle()
}
